﻿using System.Runtime.Serialization;
using Ooaavee.Data.Model;

namespace Ooaavee.Data.Queries
{
    [DataContract]
    public sealed class QueryResultRowValue
    {
        [DataMember]
        private QueryResult _linkedData;

        [DataMember]
        public string ErrorMessage { set; get; }

        [DataMember]
        public bool Malformed { set; get; }

        [DataMember]
        public Table Table { set; get; }

        [DataMember]
        public Column Column { set; get; }

        [DataMember]
        public object Value { set; get; }

        [DataMember]
        public QueryResultRow Parent { set; get; }

        public bool IsNull
        {
            get { return Value == null; }
        }

        public bool IsLinkedDataAvailable
        {
            get { return Column.LinkedToColumns != null && Column.LinkedToColumns.Count > 0 && Value != null; }
        }

        public bool HasLinkedDataRetrieved
        {
            get { return _linkedData != null; }
        }

        public QueryResult LinkedData
        {
            get
            {
                if (IsLinkedDataAvailable == false)
                {
                    return null;
                }

                if (_linkedData == null)
                {



                    //_linkedData = new ParentTableQuery(this).Execute().FirstOrNull();




                }
                return _linkedData;
            }
        }

        public override string ToString()
        {
            return Value == null ? "null" : Value.ToString();
        }

        internal bool Equals(QueryResultRowValue comp)
        {
            if (IsNull && comp.IsNull)
            {
                return true;
            }

            if (IsNull == false && comp.IsNull == false)
            {
                if (Value.Equals(comp.Value))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
